﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagement
{
    public partial class AddAndDelect : Form
    {
        public AddAndDelect()
        {
            InitializeComponent();
        }
        public AddAndDelect(int id,string title ,string author ,string createdtime)
        {
            InitializeComponent();

            this.id = id;
            textBox2.Text = title;
            textBox3.Text = author;
            textBox4.Text = createdtime;

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
        
        private void button1_Click(object sender, EventArgs e)
        {
           
            this.Close();
        }
        private int id;
        private void button2_Click(object sender, EventArgs e)
        {
            //添加与修改
            var titleName = textBox2.Text;
            var author = textBox3.Text;
            var createdTime = textBox4.Text;

            if (id > 0)
            {
                if (!string.IsNullOrEmpty(titleName))
                {

                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',CreatedTime='{2}' where Id='{3}'", titleName, author, createdTime, id);
                    var res = Means.AddorEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功", "提示");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("修改的数据不能为空", "提示");
                    }            
                    }

            }
            else
            {
                if (!string.IsNullOrEmpty(titleName))
                {
                    
                    var sql = string.Format("insert into Articles(Title,Author,CreatedTime) values('{0}','{1}','{2}')",titleName,author,createdTime);
                    var res = Means.AddorEditOrDeleteSave(sql);

                    if (res == 1)
                    {

                        MessageBox.Show("添加成功！", "提示");
                        
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败！笨蛋", "提示");
                        this.Close();
                    }

                }
                else
                {
                    MessageBox.Show("没有输入要添加的数据，憨憨！");
                }



            }










        }
    }
}
